package com.eight.controller;

import com.eight.vo.ComResult;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * @Description: 生产租车订单的消息队列
 * @Author: Van.Planifolia
 * @CreateTime: 2023-03-08  20:35
 * @Version: 1.0
 */
@RestController
@RequestMapping("/rabbit/car")
public class CarRabbitController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 创建一个Car订单的消息队列
     * @param id 需要的订单的uuid
     * @return 创建结果
     */
    @GetMapping("/makeCarOrder/{id}")
    public ComResult makeCarOrder(@PathVariable String id) {
        // 给普通交换机发送一个消息
        rabbitTemplate.convertAndSend("SE", "car.in", "车辆订单:" + id
                , message -> {
                    // 设置uuid
                    message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
                    // 设置超时时间30min
                    message.getMessageProperties().setDelay(5 * 1000 * 60);
                    // 设置消息id
                    message.getMessageProperties().setCorrelationId(id);
                    return message;
                }, new CorrelationData(id));
        return new ComResult(2000, "投递成功");
    }
}
